from flask import Flask, request, jsonify
from flask_session import Session
import os
from flask_restful import Api
from admin.ManagerUsers import getUsers
from users.account.updateAccount import updateAccount
from content.GetImage import GetImage
from users.article.DeleteArticle import DeleteArticle
from users.book.GetBook import GetBook
from users.book.GetLibrary import GetLibrary
from users.article.PostArticle import PostArticle
from users.account.GetAccount import GetAccount
from users.article.GetArticles import GetArticles
from users.cart.GetCart import GetCart
from users.cart.InsertCart import InsertCart
from users.order.booksReturn import booksReturn
from users.cart.removeCart import remove
from users.order.getOrders import getOrders
from users.article.GetArticleById import GetArticleById
from users.book.UpdateStack import UpdateStack
from users.book.CreateStack import CreateStack
from users.book.GetStacks import GetAllStacks
from users.book.DeleteStack import DeleteStack
from users.book.GetBooksByStack import GetBooksByStackId
from users.order.InsertOrder import InsertOrder

from users.order.orders import getUserOrders
from users.account.Login import Login
from users.account.Register import Register
from flask_cors import CORS

app = Flask(__name__)

app.secret_key = os.urandom(24)
api = Api(app)
CORS(app, resources={r"/*": {"origins": "*"}})
app.config['CORS_HEADERS'] = ['Content-Type', 'Authorization']
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
app.config["SQLALCHEMY_POOL_TIMEOUT"] = 30
app.config["SECRET_KEY"] = "25298439252095820938520"
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
app.config["SESSION_COOKIE_SAMESITE"] = "None"
app.config["SESSION_COOKIE_SECURE"] = True
Session(app)


@app.before_request
def before_request():
    if request.method == 'OPTIONS':
        # Handle OPTIONS request globally
        response = jsonify({'Allow': 'GET, POST, PUT, DELETE'})
        response.headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, DELETE'
        response.headers['Access-Control-Allow-Headers'] = 'Content-Type, Accept, Origin, X-Requested-With'
        return response


@app.after_request
def middleware_for_response(response):
    # Allowing the credentials in the response.
    response.headers.add('Access-Control-Allow-Credentials', 'true')
    return response


api.add_resource(Login, '/api/account/login')
api.add_resource(GetAccount, '/api/account/info')
api.add_resource(updateAccount, '/api/account/update')
api.add_resource(booksReturn, '/api/account/books/return')
api.add_resource(getOrders, '/api/account/orders/info')
api.add_resource(GetArticles, '/api/articles/info')
api.add_resource(GetArticleById, '/api/articles/<int:articleId>')
api.add_resource(PostArticle, '/api/articles/post')
api.add_resource(DeleteArticle, '/api/articles/delete')
api.add_resource(GetLibrary, '/api/library/info')
api.add_resource(GetBook, '/api/library/book')
api.add_resource(remove, '/api/cart/remove')
api.add_resource(InsertCart, '/api/cart/insert')
api.add_resource(GetCart, '/api/cart/get')
api.add_resource(getUserOrders, '/api/account/orders/info')
api.add_resource(InsertOrder, '/api/account/orders/insert')
api.add_resource(Register, '/api/account/register')
api.add_resource(GetImage, '/assets/<path:path>')
api.add_resource(UpdateStack, '/api/stack/<int:stackId>')
api.add_resource(CreateStack, '/api/stack/create')
api.add_resource(GetAllStacks, '/api/stack/info')
api.add_resource(DeleteStack, '/api/stack/delete')
api.add_resource(GetBooksByStackId, '/api/books/byStackId')

api.add_resource(getUsers, '/admin/users')

if __name__ == '__main__':
    app.run(debug=True)
